package com.ruoyi.cases.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.cases.domain.JlArchives;
import com.ruoyi.cases.mapper.JlArchivesMapper;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysRoleMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cases.mapper.JlArchivesTypeMapper;
import com.ruoyi.common.core.domain.entity.JlArchivesType;
import com.ruoyi.cases.service.IJlArchivesTypeService;

import javax.lang.model.element.NestingKind;

/**
 * 档案分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-20
 */
@Service
public class JlArchivesTypeServiceImpl implements IJlArchivesTypeService {
    @Autowired
    private JlArchivesTypeMapper jlArchivesTypeMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private JlArchivesMapper jlArchivesMapper;

    /**
     * 查询档案分类
     *
     * @param id 档案分类主键
     * @return 档案分类
     */
    @Override
    public JlArchivesType selectJlArchivesTypeById(Long id) {
        return jlArchivesTypeMapper.selectJlArchivesTypeById(id);
    }

    /**
     * 查询档案分类列表
     *
     * @param jlArchivesType 档案分类
     * @return 档案分类
     */
    @Override
    public List<JlArchivesType> selectJlArchivesTypeList(JlArchivesType jlArchivesType) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser.getUsername().equals("admin")) {
            return jlArchivesTypeMapper.selectJlArchivesTypeList(jlArchivesType);
        } else {
            String[] deptTypes = sysRoleMapper.getDeptTypes(loginUser.getDeptId());
            List<JlArchivesType> jlArchivesTypes = jlArchivesTypeMapper.selectJlArchivesTypeList(jlArchivesType);
            List<JlArchivesType> collect = jlArchivesTypes.stream().filter(j -> ArrayUtils.contains(deptTypes, j.getTypeNo().substring(0, 1))).collect(Collectors.toList());
            return collect;
        }
    }

    /**
     * 新增档案分类
     *
     * @param jlArchivesType 档案分类
     * @return 结果
     */
    @Override
    public int insertJlArchivesType(JlArchivesType jlArchivesType) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        jlArchivesType.setCreateBy(loginUser.getUsername());
        //判断是否存在编码
        List<JlArchivesType> jlArchivesTypes = jlArchivesTypeMapper.selectJlArchivesTypeByCode(jlArchivesType.getTypeNo());
        if (jlArchivesTypes.size() > 0) {
            throw new RuntimeException("分类编码已存在");
        }
        jlArchivesType.setCreateTime(DateUtils.getNowDate());
        return jlArchivesTypeMapper.insertJlArchivesType(jlArchivesType);
    }

    /**
     * 修改档案分类
     *
     * @param jlArchivesType 档案分类
     * @return 结果
     */
    @Override
    public int updateJlArchivesType(JlArchivesType jlArchivesType) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        jlArchivesType.setUpdateBy(loginUser.getUsername());
        List<JlArchivesType> jlArchivesTypes = jlArchivesTypeMapper.selectJlArchivesTypeByCode(jlArchivesType.getTypeNo());
        if (jlArchivesTypes.size() > 0) {
            if (!jlArchivesTypes.get(0).getId().equals(jlArchivesType.getId())) {
                throw new RuntimeException("分类编码已存在");
            }
        }
        jlArchivesType.setUpdateTime(DateUtils.getNowDate());
        return jlArchivesTypeMapper.updateJlArchivesType(jlArchivesType);
    }

    /**
     * 批量删除档案分类
     *
     * @param ids 需要删除的档案分类主键
     * @return 结果
     */
    @Override
    public int deleteJlArchivesTypeByIds(Long[] ids) {
        for (Long id : ids) {
            JlArchives jlArchive = new JlArchives();
            jlArchive.setCaseType(String.valueOf(id));

            List<JlArchives> jlArchives = jlArchivesMapper.selectJlArchivesList(jlArchive);
            if (jlArchives.size() > 0) {
                throw new RuntimeException("该分类下存在档案数据，不可删除");
            }
        }
        return jlArchivesTypeMapper.deleteJlArchivesTypeByIds(ids);
    }

    /**
     * 删除档案分类信息
     *
     * @param id 档案分类主键
     * @return 结果
     */
    @Override
    public int deleteJlArchivesTypeById(Long id) {
        return jlArchivesTypeMapper.deleteJlArchivesTypeById(id);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param archivesTypes 类型列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildTypeTreeSelect(List<JlArchivesType> archivesTypes) {
        List<JlArchivesType> typeTrees = buildTypeTree(archivesTypes);
        return typeTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param archivesTypes 类型列表
     * @return 树结构列表
     */
    @Override
    public List<JlArchivesType> buildTypeTree(List<JlArchivesType> archivesTypes) {
        List<JlArchivesType> returnList = new ArrayList<JlArchivesType>();
        List<String> tempList = archivesTypes.stream().map(JlArchivesType::getTypeNo).collect(Collectors.toList());
        for (JlArchivesType archivesType : archivesTypes) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(String.valueOf(archivesType.getParentId()))) {
                recursionFn(archivesTypes, archivesType);
                returnList.add(archivesType);
            }
        }
        if (returnList.isEmpty()) {
            returnList = archivesTypes;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<JlArchivesType> list, JlArchivesType t) {
        // 得到子节点列表
        List<JlArchivesType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (JlArchivesType tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<JlArchivesType> getChildList(List<JlArchivesType> list, JlArchivesType t) {
        List<JlArchivesType> tlist = new ArrayList<JlArchivesType>();
        Iterator<JlArchivesType> it = list.iterator();
        while (it.hasNext()) {
            JlArchivesType n = (JlArchivesType) it.next();
            if (StringUtils.isNotNull(n.getParentId())) {
                if (String.valueOf(n.getParentId()).equals(t.getTypeNo())) {
                    tlist.add(n);
                }
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<JlArchivesType> list, JlArchivesType t) {
        return getChildList(list, t).size() > 0;
    }
}
